<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <el-form-item label="分组字段">
          <el-select v-model="selectValue" multiple style="width: 300px">
            <el-option v-for="item1 in selectOptions" :key="item1.optionLabel" :label="item1.optionLabel" :value="item1.optionValue" />
          </el-select>
        </el-form-item>
        <div class="flex AIC">
          <el-form-item label="完成状态" class="ml10">
            <el-select v-model="selectValue2" style="width: 120px">
              <el-option v-for="item1 in selectOptions2" :key="item1.optionLabel" :label="item1.optionLabel" :value="item1.optionValue" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button class="primaryBth ml10" type="primary" size="default" @click="search">搜索</el-button>
          </el-form-item>
        </div>
        <el-tree-v2
          accordion
          @node-expand="nodeExpand"
          style="max-width: 600px"
          :data="treeData"
          :props="defaultProps"
          :height="600"
          @node-click="handleNodeClick"
          @current-change="currentChange"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">问题查询</h1>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" />
        <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { formateData } from '@/plugins/mixinJ'
import formDataReq from '@/utils/formDataReq'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'TechnologyTransfer'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const selectValue = ref(['stage', 'styleNo', 'process', 'styleColor'])
const selectOptions = ref([
  {
    optionLabel: '阶段',
    optionValue: 'stage'
  },
  {
    optionLabel: '型体',
    optionValue: 'styleNo'
  },
  {
    optionLabel: '工序',
    optionValue: 'process'
  },
  {
    optionLabel: '配色',
    optionValue: 'styleColor'
  }
])
const selectValue2 = ref('0')
const selectOptions2 = ref([
  {
    optionLabel: '全部',
    optionValue: '0'
  },
  {
    optionLabel: '已完成',
    optionValue: '1'
  },
  {
    optionLabel: '未完成',
    optionValue: '-1'
  }
])
const defaultProps = {
  value: 'text',
  children: 'children',
  label: 'text'
}
const currentStyleNo = ref('')
const currentStyleColor = ref('')
const currentProcess = ref('')
const currentStage = ref('')
const treeData = ref([]) as any
const handleNodeClick = (data: any, treeNode: any) => {
  getTableList(data)
}
const nodeExpand = (data: any) => {
  console.log(data)
}
const getTreeData = async () => {
  let formData = new FormData()
  selectValue.value.forEach((item: any) => {
    formData.append('groupField', item)
  })
  formData.append('finishStatus', selectValue2.value)
  formData.append('page', '1')
  formData.append('parentLevel', '-1')
  const res = await formDataReq.post('/sdStyle/tree/list', formData)
  treeData.value = res
}
const currentChange = (data: any) => {
  if (data.type === 'stage') {
    currentStage.value = data.title
    currentStyleNo.value = ''
    currentProcess.value = ''
    currentStyleColor.value = ''
  }
  if (data.type === 'styleNo') {
    currentStyleNo.value = data.title
    currentProcess.value = ''
    currentStyleColor.value = ''
  }
  if (data.type === 'process') {
    currentProcess.value = data.title
    currentStyleColor.value = ''
  }
  if (data.type === 'styleColor') {
    currentStyleColor.value = data.title
  }
}
onMounted(() => {
  getTreeData()
})

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入颜色',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入阶段',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工序',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入采集人',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '采集日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any
const search = () => {
  console.log(selectValue.value)
  getTreeData()
}

// 表一模块
// 表一按钮
const tableHeader = ref([
  {
    label: '型体',
    prop: 'styleNo'
  },
  {
    label: '颜色',
    prop: 'styleColor'
  },
  {
    label: '阶段',
    prop: 'stage'
  },
  {
    label: '工序',
    prop: 'process'
  },
  {
    label: '类型',
    prop: 'orderType'
  },
  {
    label: '计划上线日',
    prop: 'planDate'
  },
  {
    label: '数量',
    prop: 'orderQty'
  },
  {
    label: '欠数',
    prop: 'remainQty'
  },
  {
    label: '实际完成',
    prop: 'finishQty'
  },
  {
    label: '采集人',
    prop: 'sysUser'
  },
  {
    label: '采集日期',
    prop: 'sysDate'
  },
  {
    label: '重做备注',
    prop: 'redoReason'
  }
])
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    let formData = new FormData()
    formList.value[0].inputValue === '' ? formData.append('styleNo', currentStyleNo.value) : formData.append('styleNo', formList.value[0].inputValue)
    formList.value[1].inputValue === ''
      ? formData.append('styleColor', currentStyleColor.value)
      : formData.append('styleColor', formList.value[1].inputValue)
    formList.value[2].inputValue === '' ? formData.append('stage', currentStage.value) : formData.append('stage', formList.value[2].inputValue)
    formList.value[3].inputValue === '' ? formData.append('process', currentProcess.value) : formData.append('process', formList.value[3].inputValue)

    formData.append(
      'sysUser',
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入采集人'
      )?.inputValue
    )
    formData.append(
      'startMonth',
      formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '采集日期')
        ?.dateValue?.[0] || ''
    )
    formData.append(
      'endMonth',
      formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '采集日期')
        ?.dateValue?.[1] || ''
    )
    selectValue.value.forEach((item: any) => {
      formData.append('groupField', item)
    })
    formData.append('finishStatus', selectValue2.value)

    const res = (await fileRequest.post(`/sdStyle/exportHead`, formData)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async (treeNode: any = {}) => {
  isLoading.value = true
  let formData = new FormData()
  if (formList.value[0].inputValue || formList.value[1].inputValue || formList.value[2].inputValue || formList.value[3].inputValue) {
    formData.append('styleNo', formList.value[0].inputValue)
    formData.append('styleColor', formList.value[1].inputValue)
    formData.append('stage', formList.value[2].inputValue)
    formData.append('process', formList.value[3].inputValue)
  } else {
    formData.append('styleNo', currentStyleNo.value)
    formData.append('styleColor', currentStyleColor.value)
    formData.append('stage', currentStage.value)
    formData.append('process', currentProcess.value)
  }
  formData.append(
    'sysUser',
    formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入采集人'
    )?.inputValue
  )
  formData.append(
    'startMonth',
    formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '采集日期')
      ?.dateValue?.[0] || ''
  )
  formData.append(
    'endMonth',
    formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '采集日期')
      ?.dateValue?.[1] || ''
  )
  selectValue.value.forEach((item: any) => {
    formData.append('groupField', item)
  })
  formData.append('finishStatus', selectValue2.value)

  formData.append('pageNum', String(currentPage.value))
  formData.append('pageSize', String(pageSize.value))
  let tableDataList = await formDataReq.post<any, any>('/sdStyle/queryHeadList', formData)
  isLoading.value = false
  tableData.value = tableDataList.data.list
  total.value = tableDataList.data.total
  //   currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList(false)
  }
)
</script>

<style scoped>
:deep(.el-tree-node:focus > .el-tree-node__content) {
  color: white;
  background-color: #f89027;
}
</style>
